﻿using AutoMapper;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class DepartmentController : EntityController<Department, string, DepartmentSearch, IDepartmentViewModel, IDepartmentAppService>, IDepartmentController
    {
        public override string ControllerName
        {
            get { return ControllerConst.DEPARTMENT; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public DepartmentController()
        {
            this.Model = ModelFactory.CreateModel<IDepartmentViewModel>();
        }

        [ActionName(ActionConst.INDEX)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "department_query")]
        public ActionResult Index()
        {
            return View("~/Views/System/v1/addDepartment.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "department_edit")]
        public JsonResult Save(DepartmentViewModel model)
        {
            BoolResult jsonResult = null;
            if (model.NoPrefixId.IsNotEmpty())
            {
                model.Item.Id = SecurityAppService.Corporation.Id + "_" + model.NoPrefixId;
            }
            BoolGenericResult<Department> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "department_query")]
        public JsonResult ListDepartment()
        {
            DepartmentJsonViewModel json = new DepartmentJsonViewModel();
            json.rows = new List<DepartmentJson>();
            IList<Department> departments = this.AppService.GetBySearch(SearchFactory.CreateDepartmentSearch(this.Model.MasterModel.CurrentUserModel.CorporationCode, SearchIdentifier.Code.CorporationCode)).ToList();
            foreach (Department dp in departments)
            {
                DepartmentJson dj = Mapper.Map<Department, DepartmentJson>(dp);
                if (!string.IsNullOrEmpty(dp.SuperiorDepartment))
                {
                    dj.SuperiorDepartment = this.AppService.GetById(dp.SuperiorDepartment).Name;
                }

                json.rows.Add(dj);
            }
            json.total = departments.Count;

            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "department_query")]
        public JsonResult GetDepartments(DepartmentViewModel model)
        {
            model.SearchParam.Name = model.SearchParam.Name.Equals("请输入部门名称") ? string.Empty : model.SearchParam.Name;
            DepartmentJsonViewModel json = new DepartmentJsonViewModel();
            json.rows = new List<DepartmentJson>();
            IList<Department> departments = this.AppService.GetBySearch(SearchFactory.CreateDepartmentSearch(model.SearchParam.ID, model.SearchParam.Name)).ToList();
            foreach (Department dp in departments)
            {
                DepartmentJson dj = Mapper.Map<Department, DepartmentJson>(dp);
                if (!string.IsNullOrEmpty(dp.SuperiorDepartment))
                {
                    dj.SuperiorDepartment = this.AppService.GetById(dp.SuperiorDepartment).Name;
                }

                json.rows.Add(dj);
            }
            json.total = departments.Count;

            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "department_query")]
        public ActionResult GetDepartments()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            return PartialView("~/Views/System/v1/partial/selectDepartmentPartial.cshtml", this.Model);
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "department_edit")]
        public JsonResult DeleteDepartment(string selected)
        {
            BoolGenericResult<Department> result = null;
            List<DepartmentJson> departments = JsonConvert.DeserializeObject<List<DepartmentJson>>(selected);

            foreach (DepartmentJson json in departments)
            {
                Department d = this.AppService.GetById(json.Id);
                result = this.AppService.Delete(d);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
